package weaver.interfaces.zwgk.action;

import com.alibaba.fastjson.JSONObject;
import com.weaver.general.BaseBean;
import com.weaver.general.Util;
import weaver.conn.RecordSet;
import weaver.interfaces.workflow.action.Action;
import weaver.interfaces.zwgk.utils.WebServiceUtil;
import weaver.soa.workflow.request.RequestInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2022-09-12
 * @Description: 销售订单流程调用核检/创建接口
 * @ModifyLog:
 **/

public class WeaxsddAction extends BaseBean implements Action {

    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        RecordSet rs = new RecordSet();
        writeLog( ">>>>>>WeaxsddAction>>>>>" + requestid );
        try {
            Map<String, String> masterMap = new HashMap<>();
            rs.executeQuery("select * from " + tablename + " where requestid=?", requestid);
            rs.next();
            String mainid = Util.null2String(rs.getString("id"));
            String ddlx = Util.null2String(rs.getString("ddlx"));//订单类型
            String xsdddh = Util.null2String(rs.getString("xsdddh"));//销售订单单号
            String ddrq = Util.null2String(rs.getString("ddrq"));//订单日期
            String ckkhbh = Util.null2String(rs.getString("ckkhbh"));//参考客户编号
            String shkh = Util.null2String(rs.getString("shkh"));//送货客户
            String shkhjc = Util.null2String(rs.getString("shkhjc"));//送货客户简称
            String xsddxz = Util.null2String(rs.getString("xsddxz"));//销售订单性质
            String khddh = Util.null2String(rs.getString("khddh"));//客户订单号
            String hszje = Util.null2String(rs.getString("hszje"));//含税总金额
            String sqrgh = Util.null2String(rs.getString("sqrgh"));//申请人工号
            String sqbmbm = Util.null2String(rs.getString("sqbmbm"));//申请部门
            String sz = Util.null2String(rs.getString("sz"));//税种
            String bz = Util.null2String(rs.getString("bz"));//币种
            String hl2 = Util.null2String(rs.getString("hl2"));//汇率2
            String jgtj = Util.null2String(rs.getString("jgtj"));//价格条件
            String sktj = Util.null2String(rs.getString("sktj"));//收款条件
            String sqzje = Util.null2String(rs.getString("sqzje"));//税前总金额
            String hth = Util.null2String(rs.getString("hth"));//合同号
            String zlly = Util.null2String(rs.getString("zlly"));//资料来源
            String jkmc = Util.null2String(rs.getString("jkmc"));//接口名称

            masterMap.put("oea00", ddlx);
            masterMap.put("oea01", xsdddh);
            masterMap.put("oea02", ddrq);
            masterMap.put("oea03", ckkhbh);
            masterMap.put("oea04", shkh);
            masterMap.put("oea05", "1");
            masterMap.put("oea08", xsddxz);
            masterMap.put("oea10", khddh);
            masterMap.put("oea1008", hszje);
            masterMap.put("oea14", sqrgh);
            masterMap.put("oea15", sqbmbm);
            masterMap.put("oea21", sz);
            masterMap.put("oea23", bz);
            masterMap.put("oea24", hl2);
            masterMap.put("oea31", jgtj);
            masterMap.put("oea32", sktj);
            masterMap.put("oea032", shkhjc);
            masterMap.put("oea61", sqzje);
            masterMap.put("oeaud07", hth);
            masterMap.put("oea11", zlly);
            masterMap.put("oea12", xsdddh);
            writeLog(">>>>>>>>>>>>主表字段信息=" + masterMap.toString()+"<br/>");
            List<Map<String, String>> detailMapList = new ArrayList<>();
            rs.executeQuery("select * from " + tablename + "_dt1 where mainid=?  ", mainid);
            int count = 1;
            while (rs.next()) {
                Map<String, String> map = new HashMap<>();
                map.put("oeb03", count+"");
                map.put("oeb04", Util.null2String(rs.getString("ljbh")));
                map.put("oeb05", Util.null2String(rs.getString("xsdw")));
                map.put("oeb06", Util.null2String(rs.getString("cpxh")));
                map.put("oeb12", Util.null2String(rs.getString("xssl")));
                map.put("oeb13", Util.null2String(rs.getString("dj")));
                map.put("oeb14", Util.null2String(rs.getString("sqje")));
                map.put("oeb14t", Util.null2String(rs.getString("hsje")));
                map.put("oeb15", Util.null2String(rs.getString("ydjhrq")));
                map.put("oeb16", Util.null2String(rs.getString("pdjhrq")));
                map.put("oeb11", Util.null2String(rs.getString("khcpbh")));
                writeLog(">>>>>>>>>>>>明细字段信息=" + map.toString()+"<br/>");
                detailMapList.add(map);
                count++;
            }
            if ("".equals( jkmc )) {
                requestInfo.getRequestManager().setMessageid( "111111" );
                requestInfo.getRequestManager().setMessagecontent( "接口返回异常信息：接口名称不能为空" );
                return Action.FAILURE_AND_CONTINUE;
            }
            JSONObject resultJson = WebServiceUtil.Axmt410_icdWork(jkmc, masterMap, detailMapList);
            writeLog(">>>>>>>>>>>>接口返回结果=" + resultJson.toString());
            String code = Util.null2String( resultJson.getString( "code" ) );
            String description = Util.null2String( resultJson.getString( "description" ) );
            if (!"0".equals( code )) {
                requestInfo.getRequestManager().setMessageid( "111111" );
                requestInfo.getRequestManager().setMessagecontent( "接口返回异常信息：" + description );
                return Action.FAILURE_AND_CONTINUE;
            }
            return Action.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            requestInfo.getRequestManager().setMessageid( "111111" );
            requestInfo.getRequestManager().setMessagecontent( "Action接口异常！" );
            return Action.FAILURE_AND_CONTINUE;
        }

    }
}
